﻿// 这段 MFC 示例源代码演示如何使用 MFC Microsoft Office Fluent 用户界面
// (“Fluent UI”)。该示例仅供参考，
// 用以补充《Microsoft 基础类参考》和
// MFC C++ 库软件随附的相关电子文档。
// 复制、使用或分发 Fluent UI 的许可条款是单独提供的。
// 若要了解有关 Fluent UI 许可计划的详细信息，请访问
// https://go.microsoft.com/fwlink/?LinkId=238214.
//
// 版权所有(C) Microsoft Corporation
// 保留所有权利。

// CheckerDoc.cpp: CheckerDoc 类的实现
//

#include "stdafx.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "zChecker.h"
#endif

#include "MainFrm.h"
#include "CheckerDoc.h"
#include "CheckerView.h"

#include "CheckElementLibraryDialog.h"
#include "ModuleDialog.h"
#include "ModuleLibraryDialog.h"
#include "OperatorLibraryDialog.h"
#include "CheckCaseLibraryDialog.h"

#include "OperatorDialog.h"

#include "CheckModelLibraryDialog.h"
#include "CheckModelEditDialog.h"

#include "CheckProjectNewDialog.h"
#include "CheckProjectBrowserDialog.h"
//#include "CheckProjectPropertyDialog.h"
#include "CheckProjectRadiationRunDialog.h"
#include "CheckProjectLibraryDialog.h"
#include "CheckProjectLibrary2Dialog.h"

#include "DataProductLibraryDialog.h"
#include "FileExplorerDialog.h"
#include "FolderExplorerDialog.h"

#include "RasterPsnrDialog.h"
#include "RasterGLCMDialog.h"
#include "RasterRadiationDialog.h"
#include "LandsatRadiationDialog.h"

#include "CheckProjectRunDialog.h"


#include <propkey.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CheckerDoc

//int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM /*lParam*/, LPARAM lpData)
//{
//	if (uMsg == BFFM_INITIALIZED)
//	{
//		if (m_lpszInitialPath != NULL)  //m_lpszInitialPath 就是初始path
//		{
//			SendMessage(hwnd, BFFM_SETSELECTION, (WPARAM)TRUE,
//				(LPARAM)m_lpszInitialPath);
//		}
//	}
//
//	return 0;
//}

int CALLBACK SHBrowseForFolderCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
	char szPath[] = "E:\\Project\\Apps\\zChecker\\Data\\Database\\gdb\\checker.gdb";
	switch (uMsg)
	{

	case BFFM_INITIALIZED:    //初始化消息BFFM_INITIALIZED
		::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)szPath);   //传递默认打开路径 break;
	case BFFM_SELCHANGED:    //选择路径变化，BFFM_SELCHANGED
	{
		char curr[MAX_PATH];
		SHGetPathFromIDList((LPCITEMIDLIST)lParam, curr);
		::SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, (LPARAM)&curr[0]);
	}
	break;
	default:
		break;
	}

	return 0;
}

IMPLEMENT_DYNCREATE(CheckerDoc, CDocument)

BEGIN_MESSAGE_MAP(CheckerDoc, CDocument)
	ON_COMMAND(ID_BTN_PROJECT_NEW, &CheckerDoc::OnBtnProjectNew)
	ON_UPDATE_COMMAND_UI(ID_BTN_PROJECT_NEW, &CheckerDoc::OnUpdateBtnProjectNew)
	ON_COMMAND(ID_BTN_PROJECT_OPEN, &CheckerDoc::OnBtnProjectOpen)
	ON_UPDATE_COMMAND_UI(ID_BTN_PROJECT_OPEN, &CheckerDoc::OnUpdateBtnProjectOpen)
	ON_COMMAND(ID_BTN_PROJECT_SAVE, &CheckerDoc::OnBtnProjectSave)
	ON_UPDATE_COMMAND_UI(ID_BTN_PROJECT_SAVE, &CheckerDoc::OnUpdateBtnProjectSave)
	ON_COMMAND(ID_BTN_PROJECT_CLOSE, &CheckerDoc::OnBtnProjectClose)
	ON_UPDATE_COMMAND_UI(ID_BTN_PROJECT_CLOSE, &CheckerDoc::OnUpdateBtnProjectClose)
	ON_COMMAND(ID_BTN_CASE_IMPORT, &CheckerDoc::OnBtnCaseImport)
	ON_UPDATE_COMMAND_UI(ID_BTN_CASE_IMPORT, &CheckerDoc::OnUpdateBtnCaseImport)
	ON_COMMAND(ID_BTN_CASE_LIBRARY, &CheckerDoc::OnBtnCaseLibrary)
	ON_UPDATE_COMMAND_UI(ID_BTN_CASE_LIBRARY, &CheckerDoc::OnUpdateBtnCaseLibrary)
	ON_COMMAND(ID_BTN_MODEL_LIBRARY, &CheckerDoc::OnBtnModelLibrary)
	ON_UPDATE_COMMAND_UI(ID_BTN_MODEL_LIBRARY, &CheckerDoc::OnUpdateBtnModelLibrary)
	ON_COMMAND(ID_BTN_MODEL_EDIT, &CheckerDoc::OnBtnModelEdit)
	ON_COMMAND(ID_BTN_CHECK_ELEMENT_LIBRARY, &CheckerDoc::OnBtnCheckElementLibrary)
	ON_UPDATE_COMMAND_UI(ID_BTN_CHECK_ELEMENT_LIBRARY, &CheckerDoc::OnUpdateBtnCheckElementLibrary)
	ON_COMMAND(ID_BTN_CHECK_RUN, &CheckerDoc::OnBtnCheckRun)
	ON_UPDATE_COMMAND_UI(ID_BTN_CHECK_RUN, &CheckerDoc::OnUpdateBtnCheckRun)
	ON_COMMAND(ID_BTN_CHECK_RESULT, &CheckerDoc::OnBtnResult)
	ON_UPDATE_COMMAND_UI(ID_BTN_CHECK_RESULT, &CheckerDoc::OnUpdateBtnResult)
	ON_COMMAND(ID_BTN_OPERATOR_LIBRARY, &CheckerDoc::OnBtnOperatorLibrary)
	ON_UPDATE_COMMAND_UI(ID_BTN_OPERATOR_LIBRARY, &CheckerDoc::OnUpdateBtnOperatorLibrary)
	ON_COMMAND(ID_BTN_SYS_CONFIG, &CheckerDoc::OnBtnSysConfig)
	ON_UPDATE_COMMAND_UI(ID_BTN_SYS_CONFIG, &CheckerDoc::OnUpdateBtnSysConfig)
	ON_COMMAND(ID_BTN_SYS_MODULE, &CheckerDoc::OnBtnSysModule)
	ON_UPDATE_COMMAND_UI(ID_BTN_SYS_MODULE, &CheckerDoc::OnUpdateBtnSysModule)
	ON_COMMAND(ID_BTN_SYS_MODULE_LIBRARY, &CheckerDoc::OnBtnSysModuleLibrary)
	ON_UPDATE_COMMAND_UI(ID_BTN_SYS_MODULE_LIBRARY, &CheckerDoc::OnUpdateBtnSysModuleLibrary)
	ON_COMMAND(ID_BTN_OPEN_LAYER_TIF, &CheckerDoc::OnBtnOpenLayerTif)
	ON_UPDATE_COMMAND_UI(ID_BTN_OPEN_LAYER_TIF, &CheckerDoc::OnUpdateBtnOpenLayerTif)
	ON_COMMAND(ID_BTN_OPEN_LAYER_SHP, &CheckerDoc::OnBtnOpenLayerShp)
	ON_UPDATE_COMMAND_UI(ID_BTN_OPEN_LAYER_SHP, &CheckerDoc::OnUpdateBtnOpenLayerShp)
	ON_COMMAND(ID_BTN_OPEN_GDB, &CheckerDoc::OnBtnOpenGdb)
	ON_UPDATE_COMMAND_UI(ID_BTN_OPEN_GDB, &CheckerDoc::OnUpdateBtnOpenGdb)
	ON_COMMAND(ID_BTN_OPEN_LANDSAT05, &CheckerDoc::OnBtnOpenLandsat05)
	ON_COMMAND(ID_BTN_RASTER_PROP_PSNR, &CheckerDoc::OnBtnRasterPropPsnr)
	ON_UPDATE_COMMAND_UI(ID_BTN_RASTER_PROP_PSNR, &CheckerDoc::OnUpdateBtnRasterPropPsnr)
	ON_COMMAND(ID_BTN_RASTER_PROP_GLCM, &CheckerDoc::OnBtnRasterPropGlcm)
	ON_UPDATE_COMMAND_UI(ID_BTN_RASTER_PROP_GLCM, &CheckerDoc::OnUpdateBtnRasterPropGlcm)
	ON_COMMAND(ID_BTN_RASTER_RADIATION, &CheckerDoc::OnBtnRasterRadiation)
	ON_UPDATE_COMMAND_UI(ID_BTN_RASTER_RADIATION, &CheckerDoc::OnUpdateBtnRasterRadiation)
	ON_COMMAND(ID_BTN_LANDSAT_RADIATION, &CheckerDoc::OnBtnLandsatRadiation)
	ON_UPDATE_COMMAND_UI(ID_BTN_LANDSAT_RADIATION, &CheckerDoc::OnUpdateBtnLandsatRadiation)
	ON_COMMAND(ID_BTN_PROJECT_PROPERTY, &CheckerDoc::OnBtnProjectProperty)
	ON_UPDATE_COMMAND_UI(ID_BTN_PROJECT_PROPERTY, &CheckerDoc::OnUpdateBtnProjectProperty)
	ON_COMMAND(ID_BTN_DATA_PRODUCT_LIBRARY, &CheckerDoc::OnBtnDataProductLibrary)
	ON_UPDATE_COMMAND_UI(ID_BTN_DATA_PRODUCT_LIBRARY, &CheckerDoc::OnUpdateBtnDataProductLibrary)
	ON_COMMAND(ID_BTN_PROJECT_LOAD_FILE, &CheckerDoc::OnBtnProjectLoadFile)
	ON_UPDATE_COMMAND_UI(ID_BTN_PROJECT_LOAD_FILE, &CheckerDoc::OnUpdateBtnProjectLoadFile)
	ON_COMMAND(ID_BTN_PROJECT_LOAD_FOLDER_2, &CheckerDoc::OnBtnProjectLoadFolder)
	ON_UPDATE_COMMAND_UI(ID_BTN_PROJECT_LOAD_FOLDER_2, &CheckerDoc::OnUpdateBtnProjectLoadFolder)
	ON_COMMAND(ID_BTN_PROJECT_LOAD_LANSAT05_2, &CheckerDoc::OnBtnProjectLoadLansat05)
	ON_UPDATE_COMMAND_UI(ID_BTN_PROJECT_LOAD_LANSAT05_2, &CheckerDoc::OnUpdateBtnProjectLoadLansat05)
	
	ON_COMMAND(ID_BTN_RASTER_RADIATION_RUN, &CheckerDoc::OnBtnRasterRadiationRun)
	ON_UPDATE_COMMAND_UI(ID_BTN_RASTER_RADIATION_RUN, &CheckerDoc::OnUpdateBtnRasterRadiationRun)
	ON_COMMAND(ID_BTN_PROJECT_LIBRARY, &CheckerDoc::OnBtnProjectLibrary)
	ON_UPDATE_COMMAND_UI(ID_BTN_PROJECT_LIBRARY, &CheckerDoc::OnUpdateBtnProjectLibrary)
	ON_COMMAND(ID_BTN_CHECK_RESULT_EXPORT, &CheckerDoc::OnBtnCheckResultExport)
END_MESSAGE_MAP()


// CheckerDoc 构造/析构

CheckerDoc::CheckerDoc() noexcept
{
	// TODO: 在此添加一次性构造代码
	m_pProject = NULL;
}

CheckerDoc::~CheckerDoc()
{
	CloseProject();
}

BOOL CheckerDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: 在此添加重新初始化代码
	// (SDI 文档将重用该文档)
	SetTitle("");

	return TRUE;
}




// CheckerDoc 序列化

void CheckerDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: 在此添加存储代码
	}
	else
	{
		// TODO: 在此添加加载代码
	}
}

#ifdef SHARED_HANDLERS

// 缩略图的支持
void CheckerDoc::OnDrawThumbnail(CDC& dc, LPRECT lprcBounds)
{
	// 修改此代码以绘制文档数据
	dc.FillSolidRect(lprcBounds, RGB(255, 255, 255));

	CString strText = _T("TODO: implement thumbnail drawing here");
	LOGFONT lf;

	CFont* pDefaultGUIFont = CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT));
	pDefaultGUIFont->GetLogFont(&lf);
	lf.lfHeight = 36;

	CFont fontDraw;
	fontDraw.CreateFontIndirect(&lf);

	CFont* pOldFont = dc.SelectObject(&fontDraw);
	dc.DrawText(strText, lprcBounds, DT_CENTER | DT_WORDBREAK);
	dc.SelectObject(pOldFont);
}

// 搜索处理程序的支持
void CheckerDoc::InitializeSearchContent()
{
	CString strSearchContent;
	// 从文档数据设置搜索内容。
	// 内容部分应由“;”分隔

	// 例如:     strSearchContent = _T("point;rectangle;circle;ole object;")；
	SetSearchContent(strSearchContent);
}

void CheckerDoc::SetSearchContent(const CString& value)
{
	if (value.IsEmpty())
	{
		RemoveChunk(PKEY_Search_Contents.fmtid, PKEY_Search_Contents.pid);
	}
	else
	{
		CMFCFilterChunkValueImpl *pChunk = nullptr;
		ATLTRY(pChunk = new CMFCFilterChunkValueImpl);
		if (pChunk != nullptr)
		{
			pChunk->SetTextValue(PKEY_Search_Contents, value, CHUNK_TEXT);
			SetChunkValue(pChunk);
		}
	}
}

#endif // SHARED_HANDLERS

// CheckerDoc 诊断

#ifdef _DEBUG
void CheckerDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CheckerDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

ck::CheckProject* CheckerDoc::GetProject()
{
	return m_pProject;
}

// CheckerDoc 命令
void CheckerDoc::CloseProject()
{
	if (m_pProject)
	{
		m_pProject->Close();

		CMainFrame* pFrmMain = (CMainFrame*)AfxGetMainWnd();
		if (pFrmMain->GetSafeHwnd())
		{
			pFrmMain->m_wndProjPane.m_tree.Clear();
		}
		SetTitle("");

		m_pProject->Release();
		m_pProject = NULL;
	}
}


void CheckerDoc::OnBtnProjectNew()
{
	// TODO: 在此添加命令处理程序代码
	CCheckProjectNewDialog dlg;
	if (dlg.DoModal() == IDOK)
	{
		if (m_pProject)
		{
			CloseProject();
		}

		m_pProject = dlg.GetProject();
		if (m_pProject)
		{
			CMainFrame* pFrmMain = (CMainFrame*)AfxGetMainWnd();
			pFrmMain->m_wndProjPane.m_tree.Populate(m_pProject);
		}

		SetTitle(m_pProject->GetName());
	}

	//AfxMessageBox("OnBtnProjectNew");

	//CString strRasterPath = "E:\\Temp\\tif\\world.tif";
	//CString strShpPath = "E:\\Temp\\shp\\china.shp";

	//RasterClip(strRasterPath, strShpPath);

	//const char* szPath = "E:\\Project\\Apps\\zChecker\\Data\\Database\\mdb\\zChecker.mdb";
	//ck::ElementRepository* pElementRepository = ck::ElementRepository::GetInstance();
	//int ret = pElementRepository->Open(szPath);
	//if (ret != Z_SUCCESS)
	//{
	//	AfxMessageBox("Failure");
	//}
	
}


void CheckerDoc::OnUpdateBtnProjectNew(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}


void CheckerDoc::OnBtnProjectOpen()
{
	// TODO: 在此添加命令处理程序代码
	//AfxMessageBox("OnBtnProjectOpen");

	CCheckProjectBrowserDialog dlg;
	if (dlg.DoModal() == IDOK)
	{
		z_int32 nProjCode = dlg.GetSelectedProjectCode();
		if (nProjCode)
		{
			CloseProject();

			ck::CheckProjectManager* pProjectManager = ck::CheckProjectManager::GetInstance();
			m_pProject = pProjectManager->GetProject(nProjCode);
			if (m_pProject)
			{
				ck::ckDataProductType type = m_pProject->GetProductType();
				CMainFrame* pFrmMain = (CMainFrame*)AfxGetMainWnd();
				pFrmMain->m_wndProjPane.m_tree.Populate(m_pProject);

				SetTitle(m_pProject->GetName());
			}
		}
	}
}


void CheckerDoc::OnUpdateBtnProjectOpen(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码

}


void CheckerDoc::OnBtnProjectSave()
{
	// TODO: 在此添加命令处理程序代码
	AfxMessageBox("工程保存成功");
}


void CheckerDoc::OnUpdateBtnProjectSave(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}


void CheckerDoc::OnBtnProjectClose()
{
	// TODO: 在此添加命令处理程序代码
	CloseProject();
}


void CheckerDoc::OnUpdateBtnProjectClose(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}

void CheckerDoc::OnBtnProjectProperty()
{
	// TODO: 在此添加命令处理程序代码
	if (!m_pProject)
	{
		AfxMessageBox("尚未打开检查工程");
		return;
	}
	zxCheckProjectPropertyDialog dlg;
	dlg.SetProject(m_pProject);
	dlg.DoModal();
}


void CheckerDoc::OnUpdateBtnProjectProperty(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}

void CheckerDoc::OnBtnProjectLibrary()
{
	// TODO: 在此添加命令处理程序代码

	//AfxMessageBox("工程库");
	//CCheckProjectLibraryDialog dlg;
	CCheckProjectLibrary2Dialog dlg;
	dlg.DoModal();
}


void CheckerDoc::OnUpdateBtnProjectLibrary(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}


void CheckerDoc::OnBtnCaseImport()
{
	// TODO: 在此添加命令处理程序代码
}


void CheckerDoc::OnUpdateBtnCaseImport(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}


void CheckerDoc::OnBtnCaseLibrary()
{
	// TODO: 在此添加命令处理程序代码
	CCheckCaseLibraryDialog dlg;
	dlg.DoModal();
}


void CheckerDoc::OnUpdateBtnCaseLibrary(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}


void CheckerDoc::OnBtnModelLibrary()
{
	// TODO: 在此添加命令处理程序代码
	CCheckModelLibraryDialog dlg;
	dlg.DoModal();
}


void CheckerDoc::OnUpdateBtnModelLibrary(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}


void CheckerDoc::OnBtnModelEdit()
{
	// TODO: 在此添加命令处理程序代码

	ck::CheckModel* pCheckModel = NULL;
	ck::CheckModelManager* pModelManager = ck::CheckModelManager::GetInstance();
	pCheckModel = pModelManager->GetModel_0();

	CCheckModelEditDialog dlg;
	dlg.SetModel(pCheckModel);
	dlg.DoModal();
}


void CheckerDoc::OnBtnCheckElementLibrary()
{
	// TODO: 在此添加命令处理程序代码
	CCheckElementLibraryDialog dlg;
	dlg.DoModal();
}


void CheckerDoc::OnUpdateBtnCheckElementLibrary(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}


void CheckerDoc::OnBtnOperatorLibrary()
{
	// TODO: 在此添加命令处理程序代码
	COperatorLibraryDialog dlg;
	dlg.DoModal();
}


void CheckerDoc::OnUpdateBtnOperatorLibrary(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}


void CheckerDoc::OnBtnCheckRun()
{
	// TODO: 在此添加命令处理程序代码
	if (!m_pProject)
	{
		AfxMessageBox("检查工程尚未加载");
		return;
	}

	ck::CheckCase* pCase = m_pProject->GetCheckCase();
	if (!pCase)
	{
		AfxMessageBox("检查方案加载失败");
		return;
	}

	ck::CheckModel* pModel = NULL;
	ck::CheckModelManager* pModelManager = ck::CheckModelManager::GetInstance();
	pModel = pModelManager->GetModel(pCase->GetModeCode());
	if (pModel == NULL)
	{
		AfxMessageBox("检查方案模型失败");
		return;
	}

	ck::ckDataProductType productType = (ck::ckDataProductType)pModel->GetProductCode();
	if (productType != ck::ckDptLandsat05)
	{
		AfxMessageBox("数据产品类型不匹配");
		return;
	}

	CCheckProjectRunDialog dlg;
	dlg.SetProject(m_pProject);
	dlg.DoModal();

	{
		//测试代码
		//ck::ModuleManager* pModManager = ck::ModuleManager::GetInstance();
		//ck::Operator* pOperator = pModManager->GetModule(0)->GetOperator(0);

		//COperatorDialog dlg;
		//dlg.SetOperator(pOperator);
		//dlg.DoModal();
	}

}


void CheckerDoc::OnUpdateBtnCheckRun(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}


void CheckerDoc::OnBtnResult()
{
	// TODO: 在此添加命令处理程序代码
	AfxMessageBox("显示检查结果");
}


void CheckerDoc::OnUpdateBtnResult(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}



void CheckerDoc::OnBtnCheckResultExport()
{
	// TODO: 在此添加命令处理程序代码
	if (m_pProject == NULL)
	{
		AfxMessageBox("尚未加载检查工程");
		return;
	}
	ck::check::CheckResultSet* pResultSet = m_pProject->GetCheckResultSet();
	if (pResultSet == NULL)
	{
		AfxMessageBox("尚未执行检查");
		return;
	}

	const char*  szFilter = "xml文件 (*.xml)|*.xml";
	CString filePath("");
	CFileDialog dlg(FALSE, "xml", NULL, OFN_HIDEREADONLY, szFilter);
	if (dlg.DoModal() == IDOK)
	{
		CString strExpPath = dlg.GetPathName();
		ck::check::CheckResultWriter* pWriter = new ck::check::XMLCheckResultWriter();
		pWriter->Write(pResultSet, strExpPath);
		pWriter->Release();
	}

	
}



void CheckerDoc::OnBtnSysConfig()
{
	// TODO: 在此添加命令处理程序代码
	AfxMessageBox("系统配置");
}


void CheckerDoc::OnUpdateBtnSysConfig(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}

#include "direct.h"

void CheckerDoc::OnBtnSysModule()
{
	// TODO: 在此添加命令处理程序代码

	//char szAppPath[_MAX_PATH];
	//memset(szAppPath, 0, _MAX_PATH);
	//::GetModuleFileName(NULL, szAppPath, _MAX_PATH);
	//TRACE0(szAppPath);
	//AfxMessageBox(szAppPath);

	//GetCurrentDirectory(MAX_PATH, szAppPath);
	//AfxMessageBox(szAppPath);

	//_getcwd(szAppPath, _MAX_PATH);
	//AfxMessageBox(szAppPath);

	CModuleDialog dlg;
	dlg.DoModal();
}


void CheckerDoc::OnUpdateBtnSysModule(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
	
}



void CheckerDoc::OnBtnSysModuleLibrary()
{
	// TODO: 在此添加命令处理程序代码
	CModuleLibraryDialog dlg;
	dlg.DoModal();
}


void CheckerDoc::OnUpdateBtnSysModuleLibrary(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}


void CheckerDoc::OnBtnOpenLayerTif()
{
	// TODO: 在此添加命令处理程序代码
	const char*  szFilter = "Tif文件 (*.tif)|*.tif";
	CString filePath("");
	CFileDialog dlg(TRUE, "tif", NULL, OFN_ALLOWMULTISELECT | OFN_HIDEREADONLY, szFilter);

	int newLayerCunt = 0;
	if (dlg.DoModal() == IDOK)
	{
		CheckerView* pView = (CheckerView*)(((CMainFrame*)AfxGetMainWnd())->GetActiveView());

		POSITION pos = dlg.GetStartPosition();
		while (pos)
		{
			CString strFullPath = dlg.GetNextPathName(pos);
			IRasterLayerPtr pRasterLayer = OpenLayerTif(strFullPath);
			if (pRasterLayer)
			{
				ILayerPtr pLayer(pRasterLayer);
				pView->AddLayer(pLayer);
			}
		}
		
		pView->RefreshMap();
	}
}


void CheckerDoc::OnUpdateBtnOpenLayerTif(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}


void CheckerDoc::OnBtnOpenLayerShp()
{
	const char*  szFilter = "shp文件 (*.shp)|*.shp";
	CString filePath("");
	CFileDialog dlg(TRUE, "shp", NULL, OFN_ALLOWMULTISELECT | OFN_HIDEREADONLY, szFilter);

	int newLayerCunt = 0;
	if (dlg.DoModal() == IDOK)
	{
		CheckerView* pView = (CheckerView*)(((CMainFrame*)AfxGetMainWnd())->GetActiveView());

		POSITION pos = dlg.GetStartPosition();
		while (pos)
		{
			CString strFullPath = dlg.GetNextPathName(pos);
			IFeatureLayerPtr pFeatureLayer = OpenLayerShp(strFullPath);
			if (pFeatureLayer)
			{
				ILayerPtr pLayer(pFeatureLayer);
				pView->AddLayer(pLayer);
			}
		}

		pView->RefreshMap();
	}
}



void CheckerDoc::OnUpdateBtnOpenLayerShp(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}

IGroupLayerPtr CheckerDoc::OpenLayerLandsat5(const char* szPath)
{
	char szFolderName[_MAX_FNAME];
	memset(szFolderName, 0, _MAX_FNAME);
	_splitpath(szPath, NULL, NULL, szFolderName, NULL);

	IGroupLayerPtr pGroupLayer(CLSID_GroupLayer);
	pGroupLayer->put_Name((CComBSTR)szFolderName);

	//遍历文件夹，添加各个波段
	char szExt[_MAX_EXT];
	char szFilter[_MAX_PATH];
	_makepath(szFilter, NULL, szPath, "*", "*");
	CFileFind finder;
	BOOL ret = finder.FindFile(szFilter);
	while (ret)
	{
		ret = finder.FindNextFile();
		if (ret)
		{
			if ((!finder.IsDirectory()) && (!finder.IsDots()))
			{
				CString strPath = finder.GetFilePath();
				_splitpath(strPath, NULL, NULL, NULL, szExt);
				if (_stricmp(szExt, ".TIF") == 0)
				{
					IRasterLayerPtr pRasterLayer = OpenLayerTif(strPath);
					pRasterLayer->put_Visible(VARIANT_FALSE);
					pGroupLayer->Add(pRasterLayer);
				}
			}
		}
	}
	finder.Close();

	return pGroupLayer;
}

IRasterLayerPtr CheckerDoc::OpenLayerTif(CString strPath)
{
	char szDrv[_MAX_DRIVE];
	char szDir[_MAX_DIR];
	char szName[_MAX_FNAME];
	char szExt[_MAX_EXT];

	char szFPath[_MAX_PATH];
	char szFName[_MAX_FNAME];

	_splitpath(strPath, szDrv, szDir, szName, szExt);
	_makepath(szFPath, szDrv, szDir, NULL, NULL);
	_makepath(szFName, NULL, NULL, szName, szExt);

	IWorkspaceFactoryPtr pWorksFactory;
	HRESULT hr = pWorksFactory.CreateInstance(CLSID_RasterWorkspaceFactory);
	if (FAILED(hr))
	{
		AfxMessageBox("Error");
		return NULL;
	}

	IWorkspacePtr pWorkspace;
	hr = pWorksFactory->OpenFromFile(CComBSTR(szFPath), 0, &pWorkspace);
	if (FAILED(hr))
	{
		AfxMessageBox("Error");
		return NULL;
	}

	IRasterWorkspacePtr pRasterWorkspace(pWorkspace);
	IRasterDatasetPtr pRasterDataset = NULL;
	hr = pRasterWorkspace->OpenRasterDataset(CComBSTR(szFName), &pRasterDataset);
	if (FAILED(hr))
	{
		AfxMessageBox("Error");
		return NULL;
	}

	IRasterLayerPtr pRasterLayer(CLSID_RasterLayer);
	hr = pRasterLayer->CreateFromDataset(pRasterDataset);
	if (FAILED(hr))
	{
		AfxMessageBox("Error");
		return NULL;
	}
	else
	{
		IRasterPtr pRaster = NULL;
		IRasterDataset2Ptr pRasterDataset2 = (IRasterDataset2Ptr)pRasterDataset;
		hr = pRasterDataset2->CreateFullRaster(&pRaster);
		if (FAILED(hr))
		{
			AfxMessageBox("Failed");
		}
		else
		{
			IRasterBandCollectionPtr pRasterBandCollection = (IRasterBandCollectionPtr)pRaster;
			IRasterBandPtr pRasterBand = NULL;
			hr = pRasterBandCollection->Item(0, &pRasterBand);
			if (SUCCEEDED(hr))
			{
				//NoDataValue
				IRasterPropsPtr pRasterProps = (IRasterPropsPtr)pRasterBand;
				VARIANT var1;
				pRasterProps->get_NoDataValue(&var1);
				unsigned short noDataValue = var1.uiVal;

				VARIANT_BOOL has;
				pRasterBand->HasStatistics(&has);
				if (has!=VARIANT_TRUE)
				{
					pRasterBand->ComputeStatsAndHist();
				}
				IRasterHistogramPtr pHistogram = NULL;
				hr = pRasterBand->get_Histogram(&pHistogram);
				if (SUCCEEDED(hr))
				{
					if (pHistogram != NULL)
					{
						VARIANT val;
						pHistogram->get_Counts(&val);
						VARTYPE type = val.vt;
						if (type == (VT_ARRAY | VT_R8))
						{
							double *pData = NULL;
							SAFEARRAY* psa = val.parray;
							SafeArrayAccessData(psa, (void**)&pData);
							//psa->cbElements
							SafeArrayUnaccessData(psa);
						}
					}
				}

				IRasterStatisticsPtr pStatistics = NULL;
				hr = pRasterBand->get_Statistics(&pStatistics);
				if (SUCCEEDED(hr))
				{
					double dmax;
					pStatistics->get_Maximum(&dmax);
					double dmin;
					pStatistics->get_Minimum(&dmin);
					double stdev;
					pStatistics->get_StandardDeviation(&stdev);
				}

				IRasterStatistics2Ptr pStatistics2 = NULL;
				//pStatistics2->get_Covariances
			}
		}
	}

	return pRasterLayer;
}

IFeatureLayerPtr CheckerDoc::OpenLayerShp(CString strPath)
{
	char szDrv[_MAX_DRIVE];
	char szDir[_MAX_DIR];
	char szName[_MAX_FNAME];
	char szExt[_MAX_EXT];

	char szFPath[_MAX_PATH];
	char szFName[_MAX_FNAME];

	_splitpath(strPath, szDrv, szDir, szName, szExt);
	_makepath(szFPath, szDrv, szDir, NULL, NULL);
	_makepath(szFName, NULL, NULL, szName, szExt);

	IWorkspaceFactoryPtr pWorksFactory(CLSID_ShapefileWorkspaceFactory);
	if (!pWorksFactory)
	{
		return NULL;
	}

	IWorkspacePtr pWorkspace;

	HRESULT hr = pWorksFactory->OpenFromFile(CComBSTR(szFPath), 0, &pWorkspace);
	if (FAILED(hr))
	{
		return NULL;
	}

	IFeatureWorkspacePtr pFeatureWorkspace(pWorkspace);
	IFeatureClassPtr pFeaureClass;

	hr = pFeatureWorkspace->OpenFeatureClass(CComBSTR(szFName), &pFeaureClass);
	if (FAILED(hr))
	{
		return NULL;
	}

	IFeatureLayerPtr pFeatureLayer(CLSID_FeatureLayer);
	pFeatureLayer->putref_FeatureClass(pFeaureClass);
	pFeatureLayer->put_Name(CComBSTR(szFName));

	return pFeatureLayer;
}

void CheckerDoc::OnBtnOpenGdb()
{
	// TODO: 在此添加命令处理程序代码
	char szPath[MAX_PATH]; //存放选择的目录路径 
	CString str;

	ZeroMemory(szPath, sizeof(szPath));
	//strcpy(szPath, "E:\\Project\\Apps\\zChecker\\Data\\Database\\gdb\\checker.gdb");
	BROWSEINFO bi;
	bi.hwndOwner = AfxGetMainWnd()->m_hWnd;
	bi.pidlRoot = NULL;
	bi.pszDisplayName = szPath;
	bi.lpszTitle = "请选择GDB目录";
	bi.ulFlags = 0;
	bi.lpfn = SHBrowseForFolderCallbackProc;
	bi.lParam = 0;
	bi.iImage = 0;

	LPITEMIDLIST lp = SHBrowseForFolder(&bi);
	if (lp && SHGetPathFromIDList(lp, szPath))
	{
		size_t len = strlen(szPath);
		if (_stricmp((szPath + len - 4), ".gdb"))
		{
			AfxMessageBox("并非gdb目录，请重新选择");
		}
		else
		{
			CMainFrame* pFrmMain = (CMainFrame*)AfxGetMainWnd();
			pFrmMain->m_wndGdbPane.m_tree.SetPath(szPath);
			pFrmMain->m_wndGdbPane.m_tree.Populate();
		}
	}
}


void CheckerDoc::OnUpdateBtnOpenGdb(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}


void CheckerDoc::OnBtnOpenLandsat05()
{
	// TODO: 在此添加命令处理程序代码
	char szPath[MAX_PATH]; //存放选择的目录路径 
	CString str;

	//ZeroMemory(szPath, sizeof(szPath));
	//strcpy(szPath, "F:\\Project\\项目实施\\TJ-数据质量检查-2018\\样例数据\\RAW\\LT05_L1GS_132016_20000421_20161214_01_T2");

	//{
	//	ck::data::Landsat05 ls05;
	//	ls05.SetPath(szPath);
	//	ls05.Open();
	//	double cc = ls05.ComputeCloudCoverage();

	//	ck::data::RasterBand* pBand = NULL;
	//	pBand = ls05.GetBand(0);
	//	pBand->CreateMask();
	//	double entroy = pBand->GetEntropy();

	//	pBand->GetWidth();
	//}
	

	BROWSEINFO bi;
	bi.hwndOwner = AfxGetMainWnd()->m_hWnd;
	bi.pidlRoot = NULL;
	bi.pszDisplayName = szPath;
	bi.lpszTitle = "请选择GDB目录";
	bi.ulFlags = 0;
	bi.lpfn = NULL;
	bi.lParam = 0;
	bi.iImage = 0;

	LPITEMIDLIST lp = SHBrowseForFolder(&bi);
	if (lp && SHGetPathFromIDList(lp, szPath))
	{
		//char szFolderName[_MAX_FNAME];
		//memset(szFolderName, 0, _MAX_FNAME);
		//_splitpath(szPath, NULL, NULL, szFolderName, NULL);

		//IGroupLayerPtr pGroupLayer(CLSID_GroupLayer);
		//pGroupLayer->put_Name((CComBSTR)szFolderName);		

		////遍历文件夹，添加各个波段
		//char szExt[_MAX_EXT];
		//char szFilter[_MAX_PATH];
		//_makepath(szFilter, NULL, szPath, "*", "*");
		//CFileFind finder;
		//BOOL ret = finder.FindFile(szFilter);
		//while (ret)
		//{
		//	ret = finder.FindNextFile();
		//	if (ret)
		//	{
		//		if ((!finder.IsDirectory()) && (!finder.IsDots()))
		//		{
		//			CString strPath = finder.GetFilePath();
		//			_splitpath(strPath, NULL, NULL, NULL, szExt);
		//			if (_stricmp(szExt, ".TIF") == 0)
		//			{
		//				IRasterLayerPtr pRasterLayer = OpenLayerTif(strPath);
		//				pRasterLayer->put_Visible(VARIANT_FALSE);
		//				pGroupLayer->Add(pRasterLayer);
		//			}
		//		}
		//	}
		//}
		//finder.Close();

		IGroupLayerPtr	pGroupLayer = OpenLayerLandsat5(szPath);

		CheckerView* pView = (CheckerView*)(((CMainFrame*)AfxGetMainWnd())->GetActiveView());
		pView->AddLayer(pGroupLayer);
		pView->RefreshMap();
	}
}


void CheckerDoc::OnBtnRasterPropPsnr()
{
	// TODO: 在此添加命令处理程序代码
	CRasterPsnrDialog dlg;
	dlg.DoModal();
}


void CheckerDoc::OnUpdateBtnRasterPropPsnr(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}


void CheckerDoc::OnBtnRasterPropGlcm()
{
	// TODO: 在此添加命令处理程序代码
	CRasterGLCMDialog dlg;
	dlg.DoModal();
}


void CheckerDoc::OnUpdateBtnRasterPropGlcm(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}


void CheckerDoc::OnBtnRasterRadiation()
{
	// TODO: 在此添加命令处理程序代码
	CRasterRadiationDialog dlg;
	dlg.DoModal();
}


void CheckerDoc::OnUpdateBtnRasterRadiation(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}

void CheckerDoc::OnBtnLandsatRadiation()
{
	// TODO: 在此添加命令处理程序代码
	CLandsatRadiationDialog dlg;
	dlg.DoModal();
}


void CheckerDoc::OnUpdateBtnLandsatRadiation(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}





void CheckerDoc::OnBtnDataProductLibrary()
{
	// TODO: 在此添加命令处理程序代码
	CDataProductLibraryDialog dlg;
	dlg.DoModal();
}


void CheckerDoc::OnUpdateBtnDataProductLibrary(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}

/**
 * 向检查工程中添加单个文件
 */
void CheckerDoc::OnBtnProjectLoadFile()
{
	// TODO: 在此添加命令处理程序代码
	if (!m_pProject)
	{
		AfxMessageBox("检查工程尚未加载");
		return;
	}

	CFileExplorerDialog dlg;
	if (dlg.DoModal() == IDOK)
	{
		CString strPath = dlg.m_strPath;
		ck::EnumString& names = dlg.m_names;
		if (names.IsEmpty())
		{
			//如果names为空，说明ShellListCtrl控件上未选择任何文件或文件夹
			//将ShellTreeCtrl选择的路径作为DataSource的路径
			ck::FileDataSource* pDataSource = new ck::FileDataSource();
			pDataSource->SetPath(strPath);

			if (!AddDataSource(pDataSource))
			{
				AfxMessageBox("添加数据失败");
			}
		}
		else
		{
			const char* szName = NULL;
			names.Reset();
			while ((szName = names.Next()))
			{
				char szFullPath[_MAX_PATH];
				memset(szFullPath, 0, _MAX_PATH);
				_makepath(szFullPath, NULL, strPath, szName, NULL);

				ck::FileDataSource* pDataSource = new ck::FileDataSource();
				pDataSource->SetPath(szFullPath);

				if (!AddDataSource(pDataSource))
				{
					AfxMessageBox("添加数据失败");
				}
			}
		}
	}
}


void CheckerDoc::OnUpdateBtnProjectLoadFile(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
	
}

/**
 * 向检查工程中添加整个文件夹下的数据
 */
void CheckerDoc::OnBtnProjectLoadFolder()
{
	// TODO: 在此添加命令处理程序代码
	if (!m_pProject)
	{
		AfxMessageBox("检查工程尚未加载");
		return;
	}

	CFolderExplorerDialog dlg;
	if (dlg.DoModal() == IDOK)
	{
		CString strPath = dlg.m_strFullPath;

		ck::FolderDataSource* pDataSource = new ck::FolderDataSource();
		pDataSource->SetPath(strPath);

		RESULTCODE rc = RC_FAILURE;
		rc = pDataSource->Open();
		if (rc != RC_SUCCESS)
		{
			AfxMessageBox("添加文件夹数据源失败");
		}
		else
		{
			if (!AddDataSource(pDataSource))
			{
				AfxMessageBox("添加数据失败");
			}	
		}
	}
}

void CheckerDoc::OnUpdateBtnProjectLoadFolder(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}


/**
 * 向检查工程中添加Landsat5数据集
 */
void CheckerDoc::OnBtnProjectLoadLansat05()
{
	// TODO: 在此添加命令处理程序代码
	if (!m_pProject)
	{
		AfxMessageBox("检查工程尚未加载");
		return;
	}
	CFileExplorerDialog dlg;
	if (dlg.DoModal() == IDOK)
	{
		CString strPath = dlg.m_strPath;
		ck::EnumString& names = dlg.m_names;
		if (names.IsEmpty())
		{
			//如果names为空，说明ShellListCtrl控件上未选择任何文件或文件夹
			//将ShellTreeCtrl选择的路径作为DataSource的路径
			ck::FileDataSource* pDataSource = new ck::FileDataSource();
			pDataSource->SetPath(strPath);

			if (!AddDataSource(pDataSource))
			{
				AfxMessageBox("添加数据失败");
			}
		}
		else
		{
			const char* szName = NULL;
			names.Reset();
			while ((szName = names.Next()))
			{
				char szFullPath[_MAX_PATH];
				memset(szFullPath, 0, _MAX_PATH);
				_makepath(szFullPath, NULL, strPath, szName, NULL);

				ck::FileDataSource* pDataSource = new ck::FileDataSource();
				pDataSource->SetPath(szFullPath);

				if (!AddDataSource(pDataSource))
				{
					AfxMessageBox("添加数据失败");
				}
			}
		}
	}
}

void CheckerDoc::OnUpdateBtnProjectLoadLansat05(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}


BOOL CheckerDoc::AddDataSource(ck::DataSource* pDataSource)
{
	RESULTCODE rc = RC_SUCCESS;
	ck::DataSourceManager* pDataSourceManager = NULL;
	pDataSourceManager = ck::DataSourceManager::GetInstance();

	rc = pDataSourceManager->AddDataSource(m_pProject->GetCode(), pDataSource);
	if (rc != RC_SUCCESS)
	{
		return FALSE;
	}

	m_pProject->AddDatasets(pDataSource);
	m_pProject->AddDataSource(pDataSource);

	//添加到ProjectTreeCtrl上
	CMainFrame* pFrmMain = (CMainFrame*)AfxGetMainWnd();
	pFrmMain->m_wndProjPane.AddDataSource(pDataSource);
	return TRUE;
}

void CheckerDoc::OnBtnRasterRadiationRun()
{
	// TODO: 在此添加命令处理程序代码
	//AfxMessageBox("OnBtnRasterRadiationRun");

	if (!m_pProject)
	{
		AfxMessageBox("检查工程尚未加载");
		return;
	}

	ck::CheckCase* pCase = m_pProject->GetCheckCase();
	if (!pCase)
	{
		AfxMessageBox("检查方案加载失败");
		return;
	}

	ck::CheckModel* pModel = NULL;
	ck::CheckModelManager* pModelManager = ck::CheckModelManager::GetInstance();
	pModel = pModelManager->GetModel(pCase->GetModeCode());
	if (pModel == NULL)
	{
		AfxMessageBox("检查方案模型失败");
		return;
	}

	ck::ckDataProductType productType =  (ck::ckDataProductType)pModel->GetProductCode();
	if (productType != ck::ckDptLandsat05)
	{
		AfxMessageBox("数据产品类型不匹配");
		return;
	}

	CCheckProjectRadiationRunDialog dlg;
	dlg.SetProject(m_pProject);
	dlg.DoModal();

}


void CheckerDoc::OnUpdateBtnRasterRadiationRun(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
	
}


